import java.io.IOException;

import javax.microedition.lcdui.Form;


public class ServerConnectionForm extends Form implements ConnectionListener {

	private DisplayController m_Controller;
	private Server m_Server;
	
	public ServerConnectionForm(String title, DisplayController d) {
		super(title);
		m_Controller = d;
		init();
	}
	
	public void init() {
		append("Starting bluetooth server...");
		m_Server = new Server();
		
		final ConnectionListener l = this;
		Thread t = new Thread(new Runnable() {

			public void run() {
				try {
					m_Server.start(l);
				} catch (IOException e) {
					append("Error");
				}
			}

		});
		t.start();

	}

	public void clientConnected(Server s) {
		append("Client connected");
		HelicopterCanvas hc;
		try {
			hc = new HelicopterCanvas(m_Server);
			m_Controller.setCurrentDisplay(hc);
			hc.start();
		} catch (IOException e) {
			append("Error on starting game");
			e.printStackTrace();
		}
		
	}

}
